Ext.namespace('pages.p2p.jhnxm.jhnzhSQ.add');

pages.p2p.jhnxm.jhnzhSQ.add.jhnzhSQ = function(config) {
	Ext.apply(this, config);
	/** 操作类型 */
	this.operType = config.operType;
	this.record=config.record;
	this.initUIComponents();
	pages.p2p.jhnxm.jhnzhSQ.add.jhnzhSQ.superclass.constructor.call(this);
};
Ext.extend(pages.p2p.jhnxm.jhnzhSQ.add.jhnzhSQ, Ext.Window, {
	initUIComponents : function() {
		this.store = new Ext.data.Store();
		var sm = new Ext.grid.CheckboxSelectionModel({
			singleSelect : true,
			listeners : {
				'rowselect' : function(sm,rowIndex,record){
					var state = record.get("IS_ZHCR");
					console.log(Ext.getCmp('delete'));
					if('置换出' == state ){
						Ext.getCmp('delete').setDisabled(true);
					}else{
						Ext.getCmp('delete').setDisabled(false);
					}
				}
			}
		});
		var cm = new Ext.grid.ColumnModel([ sm, new Ext.grid.RowNumberer(), {
			header : '是否置换',
			width : 100,
			sortable : false,
			dataIndex : 'IS_ZHCR'
		}, {
			header : '债权名称',
			width : 100,
			sortable : false,
			dataIndex : 'ZQ_NAME'
		}, {
			header : '债权所属人姓名',
			width : 100,
			sortable : false,
			dataIndex : 'ZQ_OWNER_XM'
		}, {
			header : '债权所属人手机号码',
			width : 120,
			sortable : false,
			dataIndex : 'ZQ_OWNER_SJHM'
		},{
			header : '债权所属人身份证号码',
			width : 130,
			sortable : false,
			dataIndex : 'ZQ_OWNER_SFZ_NUM'
		}, {
			header : '债权开始日期',
			width : 130,
			sortable : false,
			dataIndex : 'ZQ_START_DATE'
		}, {
			header : '债权结束日期',
			width : 130,
			sortable : false,
			dataIndex : 'ZQ_END_DATE'
		}, {
			header : '债权金额',
			width : 80,
			sortable : false,
			dataIndex : 'ZQ_JE'
		}, {
			header : '已用金额',
			width : 80,
			sortable : true,
			dataIndex : 'ZQ_YYJE'
		}, {
			header : '冻结金额',
			width : 80,
			sortable : true,
			dataIndex : 'ZQ_DJJE'
		}, {
			header : '可用金额',
			width : 80,
			sortable : true,
			dataIndex : 'ZQ_KYJE'
		}, {
			header : '债权类型',
			width : 60,
			sortable : false,
			dataIndex : 'ZQ_TYPE',
			renderer : function(v){
				if('00' == v){
					return "未知";
				}else if('01' == v){
					return "债权";
				}
			}
		}, {
			header : '录入人用户名',
			width : 90,
			sortable : false,
			dataIndex : 'ZQ_OPER_USER_ID'
		}, {
			header : '录入时间',
			width : 130,
			sortable : false,
			dataIndex : 'ZQ_OPER_DATE'
		}, {
			header : '债权状态',
			width : 60,
			sortable : true,
			dataIndex : 'ZQ_STATE',
			renderer : function(v){
				if('00' == v){
					return "可用";
				}else if('01' == v){
					return "已用";
				}else if('02' == v){
					return "失效";
				}
			}
		}]);
		
		/**
		 * GridPanel
		 */
		this.grid = new Ext.grid.GridPanel({
			id : 'grid_zhsq',
			region : 'north',
			store : this.store,
			height : 195,
			title:'债权列表(置换出-置换入)',
			border : true,
			frame : false,
			viewConfig : {
				autoFill : false,
				forceFit : false,
				focusRow : function() {
					return;
				},
				getRowClass : function(record, rowIndex, rowParams, store) {
					if (record.get("TOP_LEVEL") == "1") {
						return 'ux-grid-record-onTop';
					}
				}
			},
			sm : sm,
			cm : cm,
			tbar : [ {
				id : 'zhzq',
				iconCls : 'add',
				text : '选择置换入债权',
				handler : this.onZhZq.createDelegate(this)
			}, '-',  {
				id : 'view',
				iconCls : 'view',
				text : '查看',
				handler : this.onView.createDelegate(this)
			}, '-', {
				id : 'delete',
				iconCls : 'delete',
				text : '移除',
				handler : this.onReset.createDelegate(this)
			}]
		});
		this.sqxxform = new Ext.form.FormPanel({
			region : 'center',
			layout : 'column',
			height : 150,
			labelWidth : 140,
			labelAlign : 'left',
			autoScroll : true,
			border : false,
			frame : false,
			title:'置换申请信息',
			bodyStyle : 'padding:5', 
			items : [{
				columnWidth : 1,
				layout : 'form',
				height : 10,
				border : false
			},{
				columnWidth : .5,
				layout : 'form',
				border : false,
				items : [ {
					fieldLabel : '债权提前赎回申请人',
					xtype : 'textfield',
					name : 'TQSH_SQR',
					id : 'TQSH_SQR',
					readOnly : true,
					value:parent.gyh,
					anchor : '98%'
				} ]
			},{
				columnWidth : .5,
				layout : 'form',
				border : false,
				items : [ {
					fieldLabel : '债权提前赎回置换日期',
					xtype : 'datefield',
					name : 'TQSH_SQSJ',
					id : 'TQSH_SQSJ',
					hideTrigger : true,
					readOnly:true,
					format:'Y-m-d',
					value:new Date(),
					anchor : '98%'
				} ]
			},{
				columnWidth : 1,
				layout : 'form',
				border : false,
				items : [ {
					fieldLabel : '债权提前赎回申请备注',
					xtype : 'textarea',
					name : 'TQSH_SQBZ',
					id : 'TQSH_SQBZ',
					height : 40,
					allowBlank : false, 
					maxLength : 250,
					maxLengthText : '审核备注不能超过250个字符',
					anchor : '99%'
				} ]
			}]
		});
		
		Ext.apply(this, {
			id : 'form_zhSQ',
			title : '置换债权申请',
			border : false,
			layout : 'border',
			style:{
				marginRight: 'auto',
		        marginLeft: 'auto'
			},
			width : 700,
			height : 400,
			modal : true,
			items : [this.grid,this.sqxxform],
			listeners:{
				'render' : this.initData.createDelegate(this)
			},
			tbar : [{
				text : '保存',
				iconCls : 'save',
				handler: this.onSave.createDelegate(this)
			},{
				text : '关闭',
				iconCls : 'close',
				handler:this.onCancel.createDelegate(this)
			} ]
		});
	},
	
	initData : function() {
		this.record.set('IS_ZHCR','置换出');
		this.record.set('ID',this.record.get('ID'));
		this.grid.getStore().add(this.record);
	},
	onView : function() {
		var record = this.grid.getSelectionModel().getSelected();
		if (!record) {
			ExtJ.showMessage("请选择一条需要查看的记录!");
			return;
		}
		var winv = new pages.p2p.jhnxm.jhnlr.query.JhnlrView({
			operType : 'view',
			record : record
		});
		winv.show();
	},
	onCancel : function(){
		this.close();
	},
	onSave : function(){
		var tqsh_sqr = Ext.getCmp('TQSH_SQR').getValue();
		var tqsh_sqbz =  Ext.getCmp('TQSH_SQBZ').getValue();
		if(''==tqsh_sqbz.trim()){
			Ext.Msg.alert('提示','债权提前赎回申请备注不能为空！');
			return;
		}else if(tqsh_sqbz.length>250){
			Ext.Msg.alert('提示','债权提前赎回申请备注不能超过250字！');
			return;
		}
		if('' ==tqsh_sqr){
			Ext.Msg.alert('提示','债权提前赎回申请人不能为空！');
			return;
		}
		var count=this.grid.getStore().getCount();
		if(count==2){
				var tqshOutId=this.grid.getStore().getAt(0).get('ID');
				var tqshInId=this.grid.getStore().getAt(1).get('ID');
				console.log(this.grid.getStore().getAt(0));
		}else{
			Ext.Msg.alert('提示','数据有误,请重新操作!');
			return;
		}
		
		
		if(!this.sqxxform.getForm().isValid()){
			Ext.Msg.alert('提示','页面输入的有误,请按照要求输入!');
			return;
		}
		Ext.getBody().mask("正在保存,请稍候...");
		this.sqxxform.getForm().submit({
			method:'post',
			url:ExtJ.getActionPath('jhn/saveJhnzhSQ.do'),
			params:{
				tqsh_sqr : tqsh_sqr,
				tqsh_sqbz : tqsh_sqbz,
				tqshOutId:tqshOutId,
				tqshInId:tqshInId
			},
			reset : false,
			success : function(option, action){
				var text = Ext.decode(action.response.responseText);
				if(text.isAddSuc){
					this.close();
					Ext.Msg.alert('提示','置换申请发起成功!');
				}else{
					this.close();
					var errMsg = text.msg;
					Ext.Msg.alert(errMsg);
					if(undefined != errMsg && ''!=errMsg && 'null' != errMsg){
						Ext.Msg.alert('提示', errMsg);
					}else{
						Ext.Msg.alert('提示','置换申请发起失败!');
					}
				}
				Ext.getCmp('grid').getStore().reload();
				Ext.getBody().unmask(true);
			}.createDelegate(this)
		});
	},
	onZhZq : function() {
		var count=this.grid.getStore().getCount();
		if (count==2) {
			ExtJ.showMessage("已选择置换入债权,不能重复选择!");
			return;
		}
		//var record=Ext.getCmp('grid').getStore().getAt(0);
		var win = new pages.p2p.jhnxm.jhnzhSQ.add.JhnZhZq({
			operType : 'zhzq',
			record:this.record
		});
		win.show();
	},
	onReset : function(){
		var record = this.grid.getSelectionModel().getSelected();
		if (!record) {
			ExtJ.showMessage("请选择一条需要移除的记录!");
			return;
		}
		Ext.Msg.confirm("确认","你确定要移除置换入的债权吗?",function(fn){
			if(fn == 'yes'){
				Ext.getCmp('grid_zhsq').getStore().remove(record);
			}
		}.createDelegate(this));
	}
});